package be.one.paq.ui;

import org.vaadin.navigator7.window.NavigableAppLevelWindow;

import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;

public abstract class FixedAppLevelWindow extends NavigableAppLevelWindow {

    private int pageWidth = 950;


    @Override
    public void attach() {
        super.attach();
        pageContainer.addStyleName("FixedPageTemplate-bandOuterLayoutPage");
    }

	@Override
	protected Layout createMainLayout() {
		VerticalLayout mainLayout = new VerticalLayout();
		mainLayout.setHeight("100%");
		return mainLayout;
	}

	/** Sets the page in the window, at the right place (and removes the previous one)
     * Override this (don't forget to call super) if you want to do something everytime a new page is being placed */
    @Override
    synchronized public void changePage(Component pageParam) {
        super.changePage(pageParam); // Does most of the job.
        
        prepareInnerBand(page);
        page.addStyleName("FixedPageTemplate-layoutPage"); 
    }
    
    /** Create a layout around the given component, and prepare the component to have a fixed width inside the band */
    protected Layout createBandLayout(Component innerComponent) {
        Layout result = new CssLayout();
        result.addStyleName("FixedPageTemplate-bandOuterLayout");
        if (innerComponent != null) {
            prepareInnerBand(innerComponent);
            result.addComponent(innerComponent);
        }
        return result;
    }

    /** Prepare the component to have a fixed width inside the template.
     * You probably don't call this method directly. Usually, you call createBandLayout() */
    protected void prepareInnerBand(Component innerComponent) {
        innerComponent.addStyleName("FixedPageTemplate-bandInnerLayout");
        innerComponent.setWidth(getPageWidth(), Component.UNITS_PIXELS);
    }
    
    
    public int getPageWidth() {
        return pageWidth;
    }

    /** Attention, if you change the page width, it should be done BEFORE createComponents executes (and it's called by the constructor...), because it uses the value.
     * A good way is to override createComponents in your descendant:
     * @Override
     * createComponents() {
     *     setPageWidth(1500);  // before...
     *     super.createComponent();
     * }
     * @param pageWidth
     */
    public void setPageWidth(int pageWidth) {
        this.pageWidth = pageWidth;
    }

}
